Skip to content

Convert MerkleTree timestamp from Long to java.time.Instant for better type safety#31

Merged
mitchelllisle merged 5 commits intomainfrom
copilot/fix-fdd27117-d65f-4381-9367-15645fb23502
Sep 21, 2025
Merged

Convert MerkleTree timestamp from Long to java.time.Instant for better type safety#31
mitchelllisle merged 5 commits intomainfrom
copilot/fix-fdd27117-d65f-4381-9367-15645fb23502

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Sep 21, 2025

Problem

The MerkleProof case class was using a raw Long timestamp field that stored epoch milliseconds:

case class MerkleProof(
  rootHash: String,
  recordCount: Long,
  leafHashes: Seq[String],
  timestamp: Long = System.currentTimeMillis()  // Raw milliseconds
)

This approach had several issues:

  • Poor type safety: No compile-time validation that the Long represents a timestamp
  • API ambiguity: Users couldn't tell what the Long value represented without documentation
  • Legacy approach: Using raw milliseconds instead of modern Java time APIs

Solution

Converted the timestamp field to use java.time.Instant, which provides:

case class MerkleProof(
  rootHash: String,
  recordCount: Long,
  leafHashes: Seq[String],
  timestamp: Instant = Instant.now()  // Proper timestamp type
)

Changes made:

  • Updated MerkleProof case class to use Instant instead of Long
  • Replaced System.currentTimeMillis() with Instant.now() in both default parameter and createMerkleProof method
  • Added proper import for java.time.Instant
  • Enhanced test validation to check for proper Instant type and recency

Benefits

  • Type Safety: Compile-time validation ensures timestamp fields are used correctly
  • API Clarity: Developers immediately understand they're working with a timestamp object
  • Modern Standards: Uses Java 8+ time APIs instead of legacy millisecond values
  • Better Testing: Proper validation of timestamp behavior vs. just checking non-zero values

Example Usage

val proof = MerkleTree.createMerkleProof(data, columns, idColumn)
println(proof.timestamp)  // 2025-09-21T11:18:00.036757964Z
println(proof.timestamp.isAfter(Instant.now().minusMinutes(1)))  // true

Breaking Change Notice

This is a breaking API change as the timestamp field type has changed from Long to Instant. However, this provides significant benefits in type safety and API clarity that justify the change.

All tests pass (47/47) confirming that the functionality is preserved while improving the type system.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • esm.ubuntu.com
    • Triggering command: /usr/lib/apt/methods/https (dns block)
  • repo.scala-sbt.org
    • Triggering command: /usr/lib/apt/methods/https (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: mitchelllisle <18128531+mitchelllisle@users.noreply.github.com>
Copilot AI changed the title [WIP] convert the timestamps users in MerkleTree to a proper timestamp type Convert MerkleTree timestamp from Long to java.time.Instant for better type safety Sep 21, 2025
Copilot AI requested a review from mitchelllisle September 21, 2025 11:21
@mitchelllisle mitchelllisle marked this pull request as ready for review September 21, 2025 11:31
@mitchelllisle mitchelllisle merged commit c58a7f7 into main Sep 21, 2025
2 checks passed
@mitchelllisle mitchelllisle deleted the copilot/fix-fdd27117-d65f-4381-9367-15645fb23502 branch September 21, 2025 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants